package com.tiyende.credit.loop.ui.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.tiyende.credit.loop.R
import com.tiyende.credit.loop.TiyendeApp
import com.tiyende.credit.loop.common.CommonDialog
import com.tiyende.credit.loop.databinding.TiyendeDialogCalculatorRepaymentTermBinding
import com.tiyende.credit.loop.event.TiyendeEventPointType
import com.tiyende.credit.loop.weight.pickerview.adapter.ArrayWheelAdapter
import com.tiyende.credit.loop.weight.wheelview.view.WheelView

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/13 15:41
 * @Annotation：计算器选择贷款时长
 */
class TiyendeCalculatorRepaymentTermDialog : CommonDialog {
    private lateinit var binding: TiyendeDialogCalculatorRepaymentTermBinding
    private var calculatorTitle = ""
    //事件备注
    private var eventRemark = ""
    private var dataList = arrayListOf<String>()
    private var dataUnit = ""

    private var calculatorCallback: ICalculatorDialogCallback? = null

    override fun initTiyendeWindow() {
        val params = window?.attributes
        params?.width = WindowManager.LayoutParams.MATCH_PARENT
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    constructor(context: Context) : super(context) {}

    /**
     * 选择类型
     */
    fun setTiyendeCalculatorTitle(title: String): TiyendeCalculatorRepaymentTermDialog{
        this.calculatorTitle = title
        return this
    }

    /**
     * 事件备注
     */
    fun setTiyendeCalculatorEventRemark(remark: String): TiyendeCalculatorRepaymentTermDialog{
        this.eventRemark = remark
        return this
    }

    /**
     * 数据列表
     */
    fun setTiyendeCalculatorData(dataList: ArrayList<String>): TiyendeCalculatorRepaymentTermDialog{
        this.dataList.clear()
        this.dataList.addAll(dataList)
        return this
    }

    /**
     * 数据单位
     */
    fun setTiyendeCalculatorDataUnit(unit: String): TiyendeCalculatorRepaymentTermDialog{
        this.dataUnit = unit
        return this
    }

    /**
     * 设置点击回调
     */
    fun setTiyendeCalculatorClick(callback: ICalculatorDialogCallback): TiyendeCalculatorRepaymentTermDialog{
        this.calculatorCallback = callback
        return this
    }

    override fun getTiyendeRootView(): View {
        binding = TiyendeDialogCalculatorRepaymentTermBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initTiyendeView() {
        initTiyendeDaysWheelView()
    }

    override fun initTiyendeListener() {
        setOnClickListener(binding.btCalculatorConfirm) //ok
    }

    override fun loadTiyendeData() {
        binding.txtCalculatorTitle.text = calculatorTitle
        binding.txtCalculatorUnit.text = dataUnit
        binding.wheelView.currentItem = 0
    }

    override fun onTiyendeViewClick(v: View?) {
        dismiss()
        calculatorCallback?.onConfirmItem(dataList[binding.wheelView.currentItem])
    }

    interface ICalculatorDialogCallback {
        //按钮点击
        fun onConfirmItem(result: String)
    }

    /* 初始化额度选择WheelView */
    private fun initTiyendeDaysWheelView(){
        val centerColors = intArrayOf(Color.parseColor("#FF3ED9AB"), Color.parseColor("#FF3ED9AB"))
        val outColors = intArrayOf(Color.parseColor("#ffb0e1e2"), Color.parseColor("#ff329f97"))
        binding.wheelView.setTextShaderCenter(centerColors)
        binding.wheelView.setTextShaderOut(centerColors)
        binding.wheelView.setLineSpacingMultiplier(2.5f)
        binding.wheelView.setItemsVisibleCount(5)
        binding.wheelView.setAlphaGradient(true)
        binding.wheelView.setLabel("")
        binding.wheelView.setGravity(Gravity.CENTER)
        binding.wheelView.setTypefaceCenter(TiyendeApp.app.getBlackTypeface())
        binding.wheelView.setTypefaceOut(TiyendeApp.app.getMediumTypeface())
        binding.wheelView.setTypefaceLable(Typeface.createFromAsset(context.assets,"fonts/Roboto-Regular.ttf"))
        binding.wheelView.setDividerColor(Color.parseColor("#00000000"))
        binding.wheelView.setDividerType(WheelView.DividerType.WRAP)
        binding.wheelView.adapter = ArrayWheelAdapter(dataList)
        binding.wheelView.setCyclic(true)
        //设置单位
//        binding.wheelView.setLabel(getString(R.string.tiyende_calculator_repayment_term_days_choose))
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(String.format(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_CALCULATOR_SELECT_SHOW,eventRemark))
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(String.format(TiyendeEventPointType.DIALOG.TIYENDE_DIALOG_CALCULATOR_SELECT_DISMISS,eventRemark))
    }
}